//
//  AndonImageBrowserController.m
//  AndonImagePicker
//
//  Created by 董文博 on 15/1/8.
//  Copyright (c) 2015年 OA. All rights reserved.
//

#import "AndonImageBrowserController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "CollectionViewCell.h"

@interface AndonImageBrowserController ()<UICollectionViewDataSource,UIAlertViewDelegate,UICollectionViewDelegate,UIScrollViewDelegate>
@property (nonatomic,assign) NSInteger       current;
@end

@implementation AndonImageBrowserController

-(instancetype)init
{
    if (self = [super init]) {
        UICollectionViewFlowLayout *fly = [[UICollectionViewFlowLayout alloc] init];
        fly.itemSize = [[UIScreen mainScreen] bounds].size;
        fly.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        fly.minimumInteritemSpacing = 0;
        fly.minimumLineSpacing = 0;
        self = [self initWithCollectionViewLayout:fly];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.contentSize = CGSizeMake(self.view.frame.size.width * self.images.count, self.view.frame.size.height);
    self.collectionView.pagingEnabled = YES;
    self.collectionView.scrollEnabled = YES;
    self.collectionView.maximumZoomScale = 2;
    CGPoint contentOffset = self.collectionView.contentOffset;
    contentOffset.x = self.view.frame.size.width * self.index;
    self.collectionView.contentOffset = contentOffset;
    self.title = [NSString stringWithFormat:@"%ld/%lu",self.index+1,(unsigned long)self.images.count];
    self.current = self.index + 1;
    [self.collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    
    if (self.isDelete) {
        self.navigationController.navigationBar.backItem.hidesBackButton = YES;
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:(UIBarButtonItemStyleDone) target:self action:@selector(done)];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deletePhoto)];
    }
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.current = floor(self.collectionView.contentOffset.x/self.view.frame.size.width - 0.5)+2;
    self.title = [NSString stringWithFormat:@"%ld/%lu",(long)self.current,(unsigned long)self.images.count];
}



-(void)deletePhoto
{
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:nil message:@"确认要删除吗" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    [av show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [self.images removeObjectAtIndex:self.current-1];
        if (self.images.count == 0) {
            self.didFinish();
            [self.navigationController popViewControllerAnimated:YES];
        }
        [self.collectionView reloadData];
        self.title = [NSString stringWithFormat:@"%ld/%lu",(long)self.current,(unsigned long)self.images.count];
    }
}

-(void)done{
    self.didFinish();
    [self.navigationController popViewControllerAnimated:YES];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.images.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.imageView.image = self.images[indexPath.row];
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
